package com.qf.controller;

import java.util.Arrays;
import java.util.List;

import com.qf.entity.Stock;
import com.qf.service.StockService;
import com.qf.vo.Goods;
import com.qf.vo.StockParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.qf.entity.GoodsEntity;
import com.qf.service.GoodsService;
import com.qf.common.R;


/**
 * @author Java2102
 * @email Java2102@qf.com
 * @date 2021-07-07 16:49:33
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    @Autowired
    private StockService stockService;


    /**
     * 列表
     */
    @RequestMapping("/list")
    public List<GoodsEntity> list() {
        return goodsService.list();
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public GoodsEntity info(@PathVariable("id") Integer id) {
        GoodsEntity goods = goodsService.getById(id);
        return goods;
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public GoodsEntity save(@RequestBody GoodsEntity goods) {
        // 在加入之前是没有id的
        System.out.println("之前：" + goods.getId());
        boolean save = goodsService.save(goods);
        //加入之是有id的
        System.out.println("之后：" + goods.getId());


        // 保存库存吗？

        return goods;
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public Boolean update(@RequestBody GoodsEntity goods) {
        return goodsService.updateById(goods);
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids) {
        goodsService.removeByIds(Arrays.asList(ids));

        return R.success();
    }


    @RequestMapping("/saveGoodsAndGstock")
    public Goods saveGoodsAndGstock(
            @RequestBody Goods goods) {
        System.out.println("goods 之前= " + goods);
        Boolean aBoolean = goodsService.saveGoodsAndGstock(goods);
        System.out.println("goods 之后= " + goods);
        return goods;
    }

    @RequestMapping("/lockGoodsStock")
    public Boolean lockGoodsStock(@RequestBody List<StockParam> list) {
        return stockService.lockGoodsStock(list);
    }

}
